package cn.kgc.musicserver.controller;


import cn.hutool.core.util.ObjUtil;
import cn.kgc.musicserver.domain.dto.Result;
import cn.kgc.musicserver.domain.dto.ResultUtil;
import cn.kgc.musicserver.domain.entity.Consumer;
import cn.kgc.musicserver.service.IConsumerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * <p>
 * 前端用户 前端控制器
 * </p>
 *
 * @author zd
 * @since 2025-06-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/consumer")
public class ConsumerController {
    private final IConsumerService consumerService;

    @GetMapping("list")
    public Result getAllConsumerList() {
        List<Consumer> consumerList = consumerService.list();
        return ResultUtil.returnDataSuccess(consumerList);
    }

    // ================= 新增接口：管理员信息管理（/gconsumer/xxx）=================

    /**
     * 获取管理员详情
     */
    @GetMapping("/detail")
    public Result getConsumerDetail(@RequestParam Long id) {
        Consumer consumer;
        consumer = consumerService.getById(id);
        if (ObjUtil.isNotNull(consumer)) {
            return ResultUtil.returnSuccess("获取成功", consumer);
        }
        return ResultUtil.returnFail("未找到该客户");
    }

    /**
     * 分页获取管理员列表
     */
    @RequestMapping("/page")
    public Result getConsumerPage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                  @RequestParam(value = "size", defaultValue = "5") int size,
                                  @RequestParam(value = "search", required = false) String search) {
        IPage<Consumer> page = new Page<>(pageNo, size);
        page = consumerService.page(page, search != null ? new QueryWrapper<Consumer>().like("username", search) : null);
        return ResultUtil.returnDataSuccess(page);
    }
    /**
     * 添加管理员
     */
    @PostMapping("/add")
    public Result addConsumer(@RequestBody Consumer consumer) {
        boolean success = consumerService.save(consumer);
        if (success) {
            return ResultUtil.returnSuccess("添加成功");
        }
        return ResultUtil.returnFail("添加失败");
    }

    /**
     * 更新管理员
     */
    @PostMapping("/update")
    public Result updateConsumer(@RequestBody Consumer consumer) {
        boolean success = consumerService.updateById(consumer);
        if (success) {
            return ResultUtil.returnSuccess("更新成功");
        }
        return ResultUtil.returnFail("更新失败");
    }

    /**
     * 删除管理员
     */
    @GetMapping("/delete")
    public Result deleteConsumer(@RequestParam Long id) {
        boolean success = consumerService.removeById(id);
        if (success) {
            return ResultUtil.returnSuccess("删除成功");
        }
        return ResultUtil.returnFail("删除失败");
    }
}
